// pages/order/order.js
var app = getApp()
var url = app.globalData.url
var url1 = app.globalData.url1
var cur = ''
var orderId = ''
var billEmail = ""
var billTt = ""
var billSh = 0
var billType = ""
var price = ''
var orderType=''
Page({

  /**
   * 页面的初始数据
   */

  data: {
    winHeight: "", //窗口高度
    currentTab: '', //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    names: "订单",
    flag: true,
    orders: [1, 3],
    showModalStatus: false,
    color1: "#fff",
    color2: "#fff",
    noOrder: false
  },
  back() {
    wx.navigateBack(1)
  },
  todetails(e) {
    wx.navigateTo({
      url: '../orderDetail/orderDeatil?cur='+cur+'&mid=' + e.currentTarget.id,
    })
  },
  toaccess(e) {
    wx.navigateTo({
      url: '../access/access?orderid='+e.currentTarget.dataset.id,
    })
  },
  appendacess(e){
    wx.navigateTo({
      url: '../appendAccess/appendAccess?orderid=' + e.currentTarget.dataset.id,
    })
  },
  baoxiu(e){
    wx.navigateTo({
      url: '../fanxiu/fanxiu?orderId='+e.currentTarget.dataset.id,
    })
  },
  // 滚动切换标签样式
  switchTab: function(e) {
    console.log(e)
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
    cur = e.detail.current
    if (cur < 2) {
      this.getOrders(cur, 0)
    } else if (cur == 4) {
      this.getAllOrders()
    } else {
      this.getOrders(cur, 1)
    }
  },
  // 点击标题切换当前页时改变样式
  swichNav: function(e) {
    console.log(e)
    cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
    if (cur < 2) {
      this.getOrders(cur, 0)
    }  else if (cur == 4) {
      this.getAllOrders()
    } else {
      this.getOrders(cur, 1)
    }
  },
  toOrder(){
    wx.navigateTo({
      url: '../index/index',
    })
  },
  toPay(e) {
    wx.navigateTo({
      url: '../topay/topay?orderId='+e.currentTarget.dataset.id,
    })
  },
  // 查询订单
  getOrders(os, ps) {
    const that = this
    var user = wx.getStorageSync('user')
    wx.request({
      url: url1+'/orders/findOrderStatuesByUsesAccount',
      data: {
        usesAccount: user.wxUsersAccount,
        orderStatus: os, //（订单状态）
        payStatus: ps //（订单支付状态）
      },
      header: {
        'content-type': "application/json"
      },
      method: "get",
      success(res) {
        console.log('订单数据')
        console.log(res.data)
        if (res.data.length == 0) {

          that.setData({
            orders: [],
            noOrder: true
          })
        } else {
          that.setData({
            orders: res.data,
            noOrder: false
          })
        }
      }
    })
  },
  getAllOrders() {
    const that = this
    var user = wx.getStorageSync('user')
    wx.request({
      url: url1+'/orders/findOrdersByUsesAccount',
      data: {
        usesAccount: user.wxUsersAccount
      },
      header: {
        'content-type': "application/json"
      },
      method: "get",
      success(res) {
        console.log('订单数据全部')
        console.log(res.data)
        if (res.data.length == 0) {

          that.setData({
            orders: [],
            noOrder: true
          })
        } else {
          that.setData({
            orders: res.data,
            noOrder: false
          })
        }
      }
    })
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function() {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  onLoad: function() {
    var that = this;
    //  高度自适应
    cur = app.globalData.currentTab;
    if (cur < 2) {
      this.getOrders(cur, 0)
    } else if (cur == 4) {
      this.getAllOrders()
    } else {
      this.getOrders(cur, 1)
    }

    wx.getSystemInfo({
      success: function(res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
  },
  canselOrder(e) {
    const that = this
    wx.showModal({

      content: '确认取消',
      success(config) {
        if (config) {

          var user = wx.getStorageSync('user')
          wx.request({
            url: url1+'/orders/cancel',
            data: {
              ordersId: e.currentTarget.dataset.id
            },
            header: {
              'content-type': "application/json"
            },
            method: "get",
            success(res) {
              console.log('取消订单数据')
              console.log(res.data)
              if (res.data.code == 1) {
                wx.showToast({
                  title: '取消成功',
                  icon: "loading",
                  duration: 1000
                })
                that.getOrders(cur, 0)

              } else {
                wx.showToast({
                  title: '取消失败',
                  icon: "loading",
                  duration: 1000
                })
              }
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const that = this
    cur = app.globalData.currentTab
    that.setData({
      currentTab: cur
    })
    // if (cur < 2) {
    //   this.getOrders(cur, 0)
    // } else {
    //   this.getOrders(cur, 1)
    // }
  },


  kaipiao(e) {
    const that = this
    console.log(e)
    that.showPopup()
    orderId = e.currentTarget.dataset.id
    price = e.currentTarget.dataset.price
    orderType=e.currentTarget.dataset.type
    that.setData({
      orderId: e.currentTarget.dataset.id
    })
  },

  selectBilltype1() {

    console.log("单位")
    const that = this
    billType = "企业发票"
    that.setData({
      color1: "#A01DE3",
      color2: "#fff"
    })
  },
  selectBilltype2() {
    console.log("个人发票")
    billType = "个人发票"
    const that = this
    that.setData({
      color2: "#A01DE3",
      color1: "#fff"
    })
  },
  // 发票抬头
  getbillTt(e) {
    billTt = e.detail.value
  },
  // 发票税号
  getbillsh(e) {
    billSh = e.detail.value
  },
  // 邮箱
  getbillemail(e) {
    console.log(e)
    billEmail = e.detail.value
  },
  addbills() {
    const that = this
    var re = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;

    if (billEmail.length == 0 ||
      billTt.length == 0 || billType.length == 0) {
      wx.showToast({
        title: '请输入完整信息',
        icon: "loading",
        duration: 1000
      })
    } else if (!re.test(billEmail)) {
      wx.showToast({
        title: '邮箱不正确',
        icon: "loading",
        duration: 1000
      })
    } else if (billType == "单位" && billSh.length == 0) {
      wx.showToast({
        title: '请输入企业税号',
        icon: "loading",
        duration: 1000
      })
    } else {
      // 开票
      wx.request({
        url: url1+'/Invoice/KaiPIao',
        data: {
          orderId: orderId,
          invoiceType: billType,
          iHead: billTt,
          shuihao: billSh,
          youxiang: billEmail,
          iPrice: price,
          orderMenu:orderType
        },
        header: {
          'content-type': "application/json"
        },
        method: "get",
        success(res) {
          console.log('订单开票数据')
          console.log(res.data)
          if (res.data.code == 1) {
            wx.showToast({
              title: '开票成功',
              icon: "loading",
              duration: 1000
            })
            that.popupTap()
            that.getOrders(cur, 1)

          } else {
            wx.showToast({
              title: '开票失败',
              icon: "loading",
              duration: 1000
            })
          }
        }
      })

    }
  },
  toEngineer(e) {
    console.log("联系师傅"+e)
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel,
    })
  },
  popupTap() {
    let popupComponent = this.selectComponent('.J_Popup');

    popupComponent && popupComponent.toggle(false);
  },

  showPopup() {
    let popupComponent = this.selectComponent('.J_Popup');
    /*popupComponent && popupComponent.show();*/
    popupComponent && popupComponent.toggle(true);
  },
  hidePopup() {
    let popupComponent = this.selectComponent('.J_Popup');
    //popupComponent && popupComponent.hide();
    popupComponent && popupComponent.toggle();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})